<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Account extends MX_Controller {


	function __construct(){

        parent::__construct();
		$this->load->library('auth/tank_auth');
		$this->lang->load('auth/tank_auth');
		$this->load->model('tank_auth/users');
	
		$this->load->model('account_model');
	}

	function index(){
		echo 'account';
	}
	
	function myaccount(){
		$id_user = $this->tank_auth->get_user_id();

		$user = $this->users->get_user_by_id($id_user, TRUE); 

		$data['id_user'] = $user->id;
		$data['username'] = $user->username;
		$data['email'] = $user->email;
		$password = $user->password;
		$data['update'] = TRUE;
			
				
		
		$use_username = $this->config->item('use_username', 'tank_auth');
		if ($use_username) {
			$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length['.$this->config->item('username_min_length', 'tank_auth').']|max_length['.$this->config->item('username_max_length', 'tank_auth').']|alpha_dash');
		}
		/*$this->form_validation->set_rules('email', 'Email', 'trim|xss_clean|valid_email');*/
		
		if($id_user == '' || $this->input->post('password')){
		$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|min_length['.$this->config->item('password_min_length', 'tank_auth').']|max_length['.$this->config->item('password_max_length', 'tank_auth').']|alpha_dash');
		$this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|xss_clean|matches[password]');
		}
		
		$this->form_validation->set_message('required', '<span style="color:#E91510"> %s is required </span>');
		$this->form_validation->set_message('matches', '<span style="color:#E91510"> %s must be equal to password</span>');
		
			if ($this->form_validation->run()) {
				$data_update['username'] 	= $this->form_validation->set_value('username');
				/*$data_update['email'] 		= $this->form_validation->set_value('email');*/
				$data_update['modified'] 	= date('Y-m-d H:i:s');
				
				$this->users->myaccount($id_user, $data_update);
					
					if($this->form_validation->set_value('password') != ''){
						$hasher = new PasswordHash(
							$this->config->item('phpass_hash_strength', 'tank_auth'),
							$this->config->item('phpass_hash_portable', 'tank_auth'));
							$hashed_password = $hasher->HashPassword($this->form_validation->set_value('password'));
	
							$data_password = array(
							'password' => $hashed_password
						);
							
						$this->users->myaccount($id_user, $data_password);
					} 
					redirect('account/myaccount');
					exit;					
								
			}
	
		
		
		//$data['content'] = $this->load->view('auth/form_edit_user', $data,TRUE);			
		//$this->load->view('auth/edit_user_form', $data);
		$data['use_username'] = $use_username;
		$data['content'] = 'account/myaccount';
		$this->load->view('common/common',$data);
		
	}
}